package com.rtwo.tio.client;

import com.rtwo.tio.client.msg.GroupMsgRequest;
import com.rtwo.tio.client.msg.JoinGroupRequest;
import com.rtwo.tio.client.msg.P2PRequest;
import com.rtwo.tio.common.BasicPacket;
import com.rtwo.tio.common.Common;
import com.rtwo.tio.common.MsgType;
import com.rtwo.tio.client.msg.LoginRequest;
import org.apache.commons.lang3.StringUtils;
import org.tio.client.ClientChannelContext;
import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.TioClient;
import org.tio.core.Node;
import org.tio.core.Tio;
import org.tio.utils.json.Json;

/**
 * Created by hasee on 2018/7/4.
 */
public class ClientStart {

    private static Node node = new Node(Common.HOST, Common.PORT);

    private static ClientTioHandlerImp clientTioHandlerImp = new ClientTioHandlerImp();

    private static ClientTioListener clientAioListener = new ClientTioListener();

    private static ReconnConf reconnConf = new ReconnConf(5000L);

    private static ClientGroupContext clientGroupContext = new ClientGroupContext(clientTioHandlerImp, clientAioListener, reconnConf);

    private static TioClient client;

    private static ClientChannelContext channelContext;

    public static void main(String[] args) throws Exception {
        clientGroupContext.setHeartbeatTimeout(Common.HEARTBEAT);
        client = new TioClient(clientGroupContext);
        channelContext = client.connect(node);
        command();

    }

    public static void command() throws Exception {
        @SuppressWarnings("resource")
        java.util.Scanner sc = new java.util.Scanner(System.in);
        int i = 1;
        StringBuilder sb = new StringBuilder();
        sb.append("使用指南:\r\n");
        sb.append(i++ + "、需要帮助，输入 '?'.\r\n");
        sb.append(i++ + "、登录，输入 'login loginname password'.\r\n");
        sb.append(i++ + "、进入群组，输入 'join group1'.\r\n");
        sb.append(i++ + "、群聊，输入 'groupMsg group1 text'.\r\n");
        sb.append(i++ + "、点对点聊天，输入 'p2pMsg loginname text'.\r\n");
        sb.append(i++ + "、退出程序，输入 'exit'.\r\n");
        System.out.println(sb);
        String line = sc.nextLine();
        while (true) {
            if ("exit".equalsIgnoreCase(line)) {
                System.out.println("Thanks for using! bye bye.");
                break;
            } else if ("?".equals(line)) {
                System.out.println(sb);
            }
            processCommand(line);
            line = sc.nextLine();
        }

        client.stop();
        System.exit(1);
    }

    public static void processCommand(String line) throws Exception {
        if (StringUtils.isBlank(line)) {
            return;
        }

        String[] args = StringUtils.split(line, " ");
        String command = args[0];

        if ("login".equalsIgnoreCase(command)) {
            String loginname = args[1];
            String password = args[2];
            LoginRequest loginRequest = new LoginRequest(loginname, password);
            String loginRequestJsonStr = Json.toJson(loginRequest);
            Tio.send(channelContext, new BasicPacket(MsgType.LOGIN_REQ, loginRequestJsonStr.getBytes()));
        }else if("p2pMsg".equalsIgnoreCase(command)){
            String toUser = args[1];
            String msg = args[2];
            P2PRequest p2PRequest = new P2PRequest(toUser, msg);
            String p2PRequestJsonStr = Json.toJson(p2PRequest);
            Tio.send(channelContext, new BasicPacket(MsgType.P2P_REQ, p2PRequestJsonStr.getBytes()));
        } else if("join".equalsIgnoreCase(command)){
            String groupName = args[1];
            JoinGroupRequest joinGroupRequest = new JoinGroupRequest(groupName);
            String joinGroupRequestJsonStr = Json.toJson(joinGroupRequest);
            Tio.send(channelContext, new BasicPacket(MsgType.GROUP_JOIN_REQ, joinGroupRequestJsonStr.getBytes()));
        } else if("groupMsg".equalsIgnoreCase(command)){
            String groupName = args[1];
            String msg = args[2];
            GroupMsgRequest groupMsgRequest = new GroupMsgRequest(groupName,msg);
            String groupMsgRequestJsonStr = Json.toJson(groupMsgRequest);
            Tio.send(channelContext, new BasicPacket(MsgType.GROUP_MSG_REQ, groupMsgRequestJsonStr.getBytes()));
        }
    }
}
